package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.BomMapper;
import com.sdry.mapper.hpf.PartMapper;
import com.sdry.mapper.hpf.ProductMapper;
import com.sdry.model.hpf.Bom;
import com.sdry.model.hpf.Part;
import com.sdry.model.hpf.Product;
import com.sdry.service.hpf.BomService;
import com.sdry.utils.StringUtil;

/** 
* bom管理的service实现类
* @author lian 
* @version 1.0
* @time 2018年10月15日 下午3:03:14
*/

@Service
public class BomServiceImp implements BomService {

	@Resource
	private BomMapper bomMapper;   //注入BOM mapper
	@Resource
	private ProductMapper productMapper;  //注入产品mapper
	@Resource
	private PartMapper partMapper; 		//注入配件mapper
	
	/**
	 * 查询产品数据总行数
	 * @param proruct 产品清单
	 * @return
	 */
	@Override
	public int countBom(Product proruct) {
		return productMapper.countProduct(proruct);
	}

	/**
	 * 分页查询产品数据
	 * @param map 参数集合
	 * @return
	 */
	@Override
	public List<Product> listPageBom(Map<String, Object> map) {
		return productMapper.listPageProduct(map);
	}

	/**
	 * 根据产品ID分页查询产品的配件信息
	 * @param map 参数集合
	 * @return
	 */
	@Override
	public List<Part> listPagePartByProduct(Map<String, Object> map) {
		List<Part> parts = bomMapper.listPagePartByProduct(map);
		List<Bom> boms = bomMapper.getBomByPid((Long)map.get("productId"));
		for(Part p : parts){
			for(Bom b : boms){
				if(p.getPaid() == b.getPaid()){
					p.setParemark3(b.getNumber()+"");
				}
			}
		}
		return parts;
	}

	/**
	 * 根据产品ID获取产品的配件总数
	 * @param proruct 产品信息
	 * @return
	 */
	@Override
	public int countPartByProduct(Product product) {
		return bomMapper.countPartByProduct(product);
	}

	/**
	 * 查询所有配件信息
	 * @return
	 */
	@Override
	public List<Part> listAllPart() {
		return partMapper.listAllPart();
	}
	
	/**
	 * 添加BOM数据
	 * @param bom 物料清单
	 * @return
	 */
	@Override
	public int insertBom(Bom bom){
		int isSuccess = 0;
		//根据产品ID查询
		Bom bom2 = bomMapper.findBomByBom(bom);
		//如果有数据，返回0
		if(bom2 != null){
			bom.setBid(bom2.getBid());
			isSuccess = bomMapper.updateBom(bom);
			//否则。直接保存
		}else{
			isSuccess = bomMapper.insertBom(bom);
		}
		return isSuccess;
	}

	/**
	 * 删除BOM数据
	 * @param bom 物料清单
	 * @return
	 */
	@Override
	public int deleteBom(Bom bom) {
		return bomMapper.deleteBom(bom);
	}

	/**       
	 * @param pid
	 * @return    
	 */
	@Override
	public List<Bom> getBomByPid(Long pid) {
		return bomMapper.getBomByPid(pid);
	}

}
